package org.example;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;

public class ReadPolygonFromJSON {
    public static void main(String[] args) {
        String filename = "latlon.json"; // 存储多边形坐标的 JSON 文件
        List<Point> polygon = new ArrayList<>();
        try (FileReader fileReader = new FileReader(filename)) {
            StringBuilder stringBuilder = new StringBuilder();
            int character;
            while ((character = fileReader.read())!= -1) {
                stringBuilder.append((char) character);
            }
            String jsonString = stringBuilder.toString();
            JSONArray jsonArray = new JSONArray(jsonString);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONArray pointArray = jsonArray.getJSONArray(i);
                double x = pointArray.getDouble(0);
                double y = pointArray.getDouble(1);
                polygon.add(new Point(x, y));
            }
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }

        // 打印读取到的多边形顶点
        for (Point p : polygon) {
            System.out.println("x: " + p.x + ", y: " + p.y);
        }
    }
}